﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Data;

namespace CJJCH.DAL
{
  public class T_INF_News
    {

        public T_INF_News()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_INF_News");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CJJCH.Model.T_INF_News model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_INF_News(");
            strSql.Append("ID,NewsTitle,NewsSource,NewsNote,TypeID,IsHot,IsTop,PlusDate,OrderNum,ManagerID)");
            strSql.Append(" values (");
            strSql.Append("@ID,@NewsTitle,@NewsSource,@NewsNote,@TypeID,@IsHot,@IsTop,@PlusDate,@OrderNum,@ManagerID)");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@NewsTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@NewsSource", SqlDbType.NVarChar,100),
					new SqlParameter("@NewsNote", SqlDbType.NText),
					new SqlParameter("@TypeID", SqlDbType.Int,4),
					new SqlParameter("@IsHot", SqlDbType.Bit,1),
					new SqlParameter("@IsTop", SqlDbType.Bit,1),
					new SqlParameter("@PlusDate", SqlDbType.DateTime),
					new SqlParameter("@OrderNum", SqlDbType.Int,4),
					new SqlParameter("@ManagerID", SqlDbType.UniqueIdentifier,16)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.NewsTitle;
            parameters[2].Value = model.NewsSource;
            parameters[3].Value = model.NewsNote;
            parameters[4].Value = model.TypeID;
            parameters[5].Value = model.IsHot;
            parameters[6].Value = model.IsTop;
            parameters[7].Value = model.PlusDate;
            parameters[8].Value = model.OrderNum;
            parameters[9].Value = model.ManagerID;


            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(CJJCH.Model.T_INF_News model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_INF_News set ");
            strSql.Append("NewsTitle=@NewsTitle,");
            strSql.Append("NewsSource=@NewsSource,");
            strSql.Append("NewsNote=@NewsNote,");
            strSql.Append("TypeID=@TypeID,");
            strSql.Append("IsHot=@IsHot,");
            strSql.Append("IsTop=@IsTop,");
            strSql.Append("Hits=@Hits,");
            strSql.Append("PlusDate=@PlusDate,");
            strSql.Append("OrderNum=@OrderNum,");
            strSql.Append("ManagerID=@ManagerID,");
            strSql.Append("BuildDate=@BuildDate");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@NewsTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@NewsSource", SqlDbType.NVarChar,100),
					new SqlParameter("@NewsNote", SqlDbType.NText),
					new SqlParameter("@TypeID", SqlDbType.Int,4),
					new SqlParameter("@IsHot", SqlDbType.Bit,1),
					new SqlParameter("@IsTop", SqlDbType.Bit,1),
					new SqlParameter("@Hits", SqlDbType.Int,4),
					new SqlParameter("@PlusDate", SqlDbType.DateTime),
					new SqlParameter("@OrderNum", SqlDbType.Int,4),
					new SqlParameter("@ManagerID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@BuildDate", SqlDbType.DateTime)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.NewsTitle;
            parameters[2].Value = model.NewsSource;
            parameters[3].Value = model.NewsNote;
            parameters[4].Value = model.TypeID;
            parameters[5].Value = model.IsHot;
            parameters[6].Value = model.IsTop;
            parameters[7].Value = model.Hits;
            parameters[8].Value = model.PlusDate;
            parameters[9].Value = model.OrderNum;
            parameters[10].Value = model.ManagerID;
            parameters[11].Value = model.BuildDate;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_INF_News ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CJJCH.Model.T_INF_News GetModel(Guid ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,NewsTitle,NewsSource,NewsNote,TypeID,IsHot,IsTop,Hits,PlusDate,OrderNum,ManagerID,BuildDate from T_INF_News ");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ID;

            CJJCH.Model.T_INF_News model = new CJJCH.Model.T_INF_News();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = new Guid(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.NewsTitle = ds.Tables[0].Rows[0]["NewsTitle"].ToString();
                model.NewsSource = ds.Tables[0].Rows[0]["NewsSource"].ToString();
                model.NewsNote = ds.Tables[0].Rows[0]["NewsNote"].ToString();
                if (ds.Tables[0].Rows[0]["TypeID"].ToString() != "")
                {
                    model.TypeID = int.Parse(ds.Tables[0].Rows[0]["TypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsHot"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsHot"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsHot"].ToString().ToLower() == "true"))
                    {
                        model.IsHot = true;
                    }
                    else
                    {
                        model.IsHot = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["IsTop"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsTop"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsTop"].ToString().ToLower() == "true"))
                    {
                        model.IsTop = true;
                    }
                    else
                    {
                        model.IsTop = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Hits"].ToString() != "")
                {
                    model.Hits = int.Parse(ds.Tables[0].Rows[0]["Hits"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PlusDate"].ToString() != "")
                {
                    model.PlusDate = DateTime.Parse(ds.Tables[0].Rows[0]["PlusDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderNum"].ToString() != "")
                {
                    model.OrderNum = int.Parse(ds.Tables[0].Rows[0]["OrderNum"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ManagerID"].ToString() != "")
                {
                    model.ManagerID = new Guid(ds.Tables[0].Rows[0]["ManagerID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BuildDate"].ToString() != "")
                {
                    model.BuildDate = DateTime.Parse(ds.Tables[0].Rows[0]["BuildDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,NewsTitle,NewsSource,NewsNote,TypeID,IsHot,IsTop,Hits,PlusDate,OrderNum,ManagerID,BuildDate ");
            strSql.Append(" FROM T_INF_News ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,NewsTitle,NewsSource,NewsNote,TypeID,IsHot,IsTop,Hits,PlusDate,OrderNum,ManagerID,BuildDate ");
            strSql.Append(" FROM T_INF_News ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_INF_News";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法

        /// <summary>
        ///  首页图书资讯
        /// </summary>
        /// <returns></returns>
        public DataSet GetNewsAgo()
        {
            StringBuilder strSQL = new StringBuilder();
            strSQL.Append(" SELECT top 11 ID,NewsTitle,NewsSource,NewsNote,TypeID,IsHot,IsTop,Hits,PlusDate,OrderNum,ManagerID,BuildDate ");
            strSQL.Append(" FROM T_INF_News order by buildDate desc");
            DataSet ds = new DataSet();
            ds = DbHelperSQL.Query(strSQL.ToString());
            return ds;
        }
    }
}
